package com.honey.mybatis.generator.condition.method;

import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.ElseBlock;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.TryCatchBlock;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.ExceptionFullyQualifiedJavaType;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.condition.ConditionConstant;
import com.honey.mybatis.generator.criteria.CriteriaConstant;

/**
 * clear 函数
 * @author Administrator
 *
 */
public class CreateCriteriaInternalMethod extends AbstractMethod {
	
	public CreateCriteriaInternalMethod(){
	}
	public CreateCriteriaInternalMethod(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	/** 方法名称 */
	private static final String METHOD_NAME ="create"+CriteriaConstant.CRITERIA_CLASS_NAME+"Internal";
	
	/** 返回参数类型 */
	private static final  FullyQualifiedJavaType RETURN_TYPE = new FullyQualifiedJavaType("T");
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		return METHOD_NAME;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return RETURN_TYPE;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		method.addBodyLine("T o = null;");
		
		IfBlock ifBlock = new IfBlock(ConditionConstant.ORED_CRITERIA+".size() > 0");
		
		TryCatchBlock tryCatchBlock = new TryCatchBlock(ExceptionFullyQualifiedJavaType.getExceptionInstance());
		tryCatchBlock.addBodyLine("o =(T) "+ConditionConstant.GENERATED_CRITERIA+".getClass().newInstance()");
		ifBlock.addBlocks(tryCatchBlock);
		ifBlock.addBodyLine(JavaKeyWord.RETURN + "o;");
		
		method.addBlocks(ifBlock);
		
		ElseBlock elseBlock = new ElseBlock();
		elseBlock.addBodyLine(JavaKeyWord.RETURN + JavaKeyWord.OPERATION_THIS.toString() + ConditionConstant.GENERATED_CRITERIA);
		method.addBlocks(elseBlock);
		
	}
	public static void main(String []args){
		CreateCriteriaInternalMethod  cm = new CreateCriteriaInternalMethod();
		System.out.println(cm.createMethod().compiledContent(0)); 
		
		System.out.println(cm.createMethod().compiledContent(0)); 
	}
}
